package cn.tedu._13weibo626.controller;

import cn.tedu._13weibo626.base.resopnse.JsonResult;
import cn.tedu._13weibo626.base.resopnse.StatusCode;
import cn.tedu._13weibo626.mapper.WeiboMapper;
import cn.tedu._13weibo626.pojo.dto.WeiboAddParam;
import cn.tedu._13weibo626.pojo.entity.Weibo;
import cn.tedu._13weibo626.pojo.vo.UserVO;
import cn.tedu._13weibo626.pojo.vo.WeiboDetailVO;
import cn.tedu._13weibo626.pojo.vo.WeiboIndexVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Slf4j
@Validated
@RestController
@Tag(name = "02-微博模块")
@RequestMapping("/v1/weibo/")
public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;
    /**
     * 1.发布微博功能
     */
    @Operation(summary = "发布微博功能")
    @ApiOperationSupport(order = 10)
    @PostMapping("insert")
    public JsonResult insert(@RequestBody WeiboAddParam weiboAddParam,HttpSession  session){
        log.debug("weiboAddParam="+weiboAddParam);
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null){
            return  new JsonResult(StatusCode.NOT_LOGIN);
        }
        Weibo weibo = new Weibo();
        BeanUtils.copyProperties(weiboAddParam,weibo);
        weibo.setCreateTime(new Date());
        weibo.setUserId(userVO.getId());
        weiboMapper.insertWeibo(weibo);
        return JsonResult.ok();
    }
    /**
     * 2.微博首页列表
     */
    @Operation(summary = "微博首页列表")
    @ApiOperationSupport(order = 20)
    @GetMapping("selectIndex")
    public JsonResult selectIndex() {
        List<WeiboIndexVO> weiboVOList = weiboMapper.selectIndex();
        return JsonResult.ok(weiboVOList);
    }
    /**
     * 3.微博详情页
     */
    @Operation(summary = "微博详情页")
    @ApiOperationSupport(order = 30)
    @GetMapping("selectById")
    public JsonResult selectById(@Range(min = 100,max = 500,message = "微博ID必须在100-500之间") Long id) {
        WeiboDetailVO weiboDetailVO = weiboMapper.selectById(id);
        return JsonResult.ok(weiboDetailVO);
    }
}
